﻿using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq.Expressions;
using Loki.Utils;

namespace Loki
{
    public static class ObjectExtensions
    {
        #region Event Helpers
        public static void WatchChange<TObserved, TObserver>(this TObserver P_Listener, TObserved P_Source, Expression<Func<TObserved, object>> P_Property, Func<TObserver, EventHandler<PropertyChangedEventArgs>> P_Handler) where TObserved : INotifyPropertyChanged
        {
            LokiServices.Event.RegisterPropertyChanged(P_Source, P_Property, P_Listener, (v, o, e) => P_Handler(v)(o, e));
        }

        public static void WatchCollectionChange<TObserved, TObserver>(this TObserver P_Listener, TObserved P_Source, Func<TObserver, EventHandler<NotifyCollectionChangedEventArgs>> P_Handler) where TObserved : INotifyCollectionChanged
        {
            LokiServices.Event.RegisterCollectionChanged(P_Source, P_Listener, (v, o, e) => P_Handler(v)(o, e));
        }

        public static void WatchStateChanged<TObserved, TObserver>(this TObserver P_Listener, TObserved P_Source, Func<TObserver, EventHandler> P_Handler) where TObserved : ICentralizedChangeTracking
        {
            LokiServices.Event.RegisterStateChanged(P_Source, P_Listener, (v, o, e) => P_Handler(v)(o, e));
        }
        #endregion
    }
}
